package it.blueocean.acanto.taglibrary.jsp.taglib.tags.commands;

import it.blueocean.acanto.taglibrary.jsp.taglib.tags.StateBeanPropertyLocator;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.StateBeansManager;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanNotFoundException;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.ComponentStateBeanPropertyNotFoundException;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.exceptions.MalformedPropertyExpressionException;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HtmlPrintHelper;
import it.blueocean.acanto.taglibrary.jsp.taglib.tags.utils.HttpUtils;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EditPropertyTag extends TagSupport
{
	private static final long serialVersionUID = 5450218043L;

	private final static Logger logger_ = LoggerFactory.getLogger(EditPropertyTag.class);

	private String property_;

	private String htmlClass_;

	private String htmlStyle_;

	private String onSubmit_;

	private String propertyExpr_;

	private String htmlClassExpr_;

	private String htmlStyleExpr_;

	private String onSubmitExpr_;

	public int doStartTag() throws JspException
	{
		evaluateExpressions();
		StateBeansManager localStateBeansManager = StateBeansManager.getInstance(this.pageContext.getSession());
		try
		{
			Object localObject = localStateBeansManager.getStateBeanProperty(new StateBeanPropertyLocator(
					this.property_));
			JspWriter localJspWriter = this.pageContext.getOut();
			String str1 = HttpUtils.generateSetPropertyURL(null, (HttpServletRequest) this.pageContext.getRequest());
			String str2 = (localObject != null) ? localObject.toString() : "";
			HtmlPrintHelper.printOpenedHtmlStartTag("a", null, null, localJspWriter);
			HtmlPrintHelper.printClosedHtmlStartTag(localJspWriter);
			HtmlPrintHelper.printOpenedHtmlStartTag("input", this.htmlClass_, this.htmlStyle_, localJspWriter);
			HtmlPrintHelper.printAttribute("type", "text", localJspWriter);
			HtmlPrintHelper.printAttribute("name", this.property_, localJspWriter);
			HtmlPrintHelper.printAttribute("value", str2, localJspWriter);
			HtmlPrintHelper.printAttribute("onkeypress", "if(event.keyCode == 13) {this.parentNode.href='" + str1 + "&"
					+ this.property_
					+ "=' + encodeURIComponent(this.value); this.parentNode.click(); return false;} else return true;",
					localJspWriter);
			HtmlPrintHelper.printAttribute("onblur", "this.value = '" + str2 + "'", localJspWriter);
			HtmlPrintHelper.printClosedHtmlEmptyTag(localJspWriter);
			HtmlPrintHelper.printHtmlEndTag("a", localJspWriter);
		}
		catch (IOException localIOException)
		{
			logger_.error(localIOException.getMessage());
		}
		catch (ComponentStateBeanNotFoundException localComponentStateBeanNotFoundException)
		{
			logger_.error(localComponentStateBeanNotFoundException.getMessage());
		}
		catch (ComponentStateBeanPropertyNotFoundException localComponentStateBeanPropertyNotFoundException)
		{
			logger_.error(localComponentStateBeanPropertyNotFoundException.getMessage());
		}
		catch (MalformedPropertyExpressionException localMalformedPropertyExpressionException)
		{
			logger_.error(localMalformedPropertyExpressionException.getMessage());
		}
		return 6;
	}

	protected void evaluateExpressions() throws JspException
	{
		this.property_ = ((String) ExpressionEvaluatorManager.evaluate("property", this.propertyExpr_, String.class,
				this, this.pageContext));
		if(this.htmlClassExpr_ != null)
			this.htmlClass_ = ((String) ExpressionEvaluatorManager.evaluate("class", this.htmlClassExpr_, String.class,
					this, this.pageContext));
		else
			this.htmlClass_ = null;
		if(this.htmlStyleExpr_ != null)
			this.htmlStyle_ = ((String) ExpressionEvaluatorManager.evaluate("style", this.htmlStyleExpr_, String.class,
					this, this.pageContext));
		else
			this.htmlStyle_ = null;
		if(this.onSubmitExpr_ != null)
			this.onSubmit_ = ((String) ExpressionEvaluatorManager.evaluate("onSubmit", this.onSubmitExpr_,
					String.class, this, this.pageContext));
		else
			this.onSubmit_ = "return true;";
	}

	public void setProperty(String paramString)
	{
		this.propertyExpr_ = paramString;
	}

	public void setClass(String paramString)
	{
		this.htmlClassExpr_ = paramString;
	}

	public void setStyle(String paramString)
	{
		this.htmlStyleExpr_ = paramString;
	}

	public String getHtmlClass()
	{
		return this.htmlClass_;
	}

	public String getHtmlStyle()
	{
		return this.htmlStyle_;
	}

	public void setOnSubmit(String paramString)
	{
		this.onSubmitExpr_ = paramString;
	}
}
